import { Link, useNavigate } from 'react-router-dom'
import { Card, Breadcrumb, Form, Button, Radio, DatePicker, Select, Table, Tag, Space, Popconfirm } from 'antd'
import { EditOutlined, DeleteOutlined } from '@ant-design/icons'
// import 'moment/locale/zh-cn'
import locale from 'antd/es/date-picker/locale/zh_CN'
// import './index.scss'
import img404 from '@/assets/error.png'
import { useChannel } from '@/hooks/useChannel';
import { useEffect, useState } from 'react'
import { getArticleListApi, deleteArticleApi } from '@/apis/article'

// const { Option } = Select
const { RangePicker } = DatePicker

const Article = () => {
    const channelListData = useChannel()
    const navigate = useNavigate()
    // 定义枚举状态
    const status = {
      1: <Tag color="warning">待审核</Tag>,
      2: <Tag color="success">审核通过</Tag>
    }
    const columns = [
        {
          title: '封面',
          dataIndex: 'cover',
          width:120,
          render: cover => {
            return <img src={cover.images[0]|| img404} width={80} height={60} alt="" />
          }
        },
        {
          title: '标题',
          dataIndex: 'title',
          width: 220
        },
        {
          title: '状态',
          dataIndex: 'status',
          render: data => {
            return status[data]
          }
        },
        {
          title: '发布时间',
          dataIndex: 'pubdate'
        },
        {
          title: '阅读数',
          dataIndex: 'read_count'
        },
        {
          title: '评论数',
          dataIndex: 'comment_count'
        },
        {
          title: '点赞数',
          dataIndex: 'like_count'
        },
        {
          title: '操作',
          render: data => {
            return (
              <Space size="middle">
                <Button type="primary" shape="circle" icon={<EditOutlined />} onClick={()=>handleEdit(data.id)}/>
                <Popconfirm
                  title="确定删除吗？"
                  onConfirm={() => handleDelete(data.id)}
                  okText="确定"
                  cancelText="取消"
                >
                  <Button
                    type="primary"
                    danger
                    shape="circle"
                    icon={<DeleteOutlined />}
                  />
                </Popconfirm>
              </Space>
            )
          }
        }
      ]
    
      // const data = [
      //     {
      //         id: '8218',
      //         comment_count: 0,
      //         cover: {
      //           images:['http://geek.itheima.net/resources/images/15.jpg'],
      //         },
      //         like_count: 0,
      //         pubdate: '2019-03-11 09:00:00',
      //         read_count: 2,
      //         status: 2,
      //         title: 'wkwebview离线化加载h5资源解决方案' 
      //     }
      // ]
      // 筛选功能准备参数
      const [reqData, setReqData] = useState({
        status: '',
        channel_id: '',
        begin_pubdata: '',
        end_pubdata: '',
        page: 1,
        per_page: 4
      })
      const [count, setCount] = useState(0)
      // 获取文章列表
      const [list, setList] = useState([])
      useEffect(()=>{
        async function getList(){
          const res = await getArticleListApi(reqData)
          console.log(res.data.results,'res===')
          if(res.message === 'OK'){
              setList(res.data.results)
              setCount(res.data.total_count - 1)
          }
        }
        getList()
      }, [reqData])
      const onFinish = (formValue)=>{
        setReqData({
          ...reqData,
          status: formValue.status === null ? '' : formValue.status,
          channel_id: formValue.channel_id || '',
          begin_pubdata: formValue.date?.[0]?.format('YYYY-MM-DD') || '',
          end_pubdata: formValue.date?.[1]?.format('YYYY-MM-DD') || '',
          page: 1,
          per_page: 4
        })
      }
      // 页数变化的时候触发的方法
      const handlePageChange = (value) => {
        console.log(reqData, 'reqData==')
        setReqData({
          ...reqData,
          page: value.current
        })
      }
      const handleDelete = async (id) => {
        console.log(id)
        await deleteArticleApi(id)
        // 删除后更新列表数据
        setReqData({
          ...reqData
        })
      }
      const handleEdit = (id) => {
        console.log('编辑')
        navigate(`/Publish?id=${id}`)
      }
  return (
    <div>
      <Card
        title={
          <Breadcrumb separator=">" items={[
            {title: <Link to="/home">首页</Link>},
            {title: '内容管理'}
          ]}>
          </Breadcrumb>
        }
        style={{ marginBottom: 20 }}
      >
        <Form initialValues={{ status: '' }} onFinish={onFinish}>
          <Form.Item label="状态" name="status">
            <Radio.Group>
              <Radio value={''}>全部</Radio>
              <Radio value={0}>草稿</Radio>
              <Radio value={1}>待审核</Radio>
              <Radio value={2}>审核通过</Radio>
              <Radio value={3}>审核失败</Radio>
            </Radio.Group>
          </Form.Item>

          <Form.Item label="频道" name="channel_id">
            <Select
              placeholder="请选择文章频道"
              style={{ width: 120 }}
              options={channelListData}
            >
            </Select>
          </Form.Item>

          <Form.Item label="日期" name="date">
            {/* 传入locale属性 控制中文显示*/}
            <RangePicker locale={locale}></RangePicker>
          </Form.Item>

          <Form.Item>
            <Button type="primary" htmlType="submit" style={{ marginLeft: 80 }}>
              筛选
            </Button>
          </Form.Item>
        </Form>
      </Card>
      {/* 筛选结果区域 */}
      <Card title={`根据筛选条件共查询到 ${list.length} 条结果：`}>
        <Table rowKey="id" columns={columns} dataSource={list} pagination={{
          total: count,
          pageSize: 3,
          onChange: handlePageChange
        }}
        />
      </Card>
    </div>
  )
}

export default Article